package com.easy.dfcapi.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.core.web.Api20Controller;
import com.easy.dfcapi.res.QuestionRes;
import com.easy.dfcapi.res.Response;
import com.easy.dfcapi.service.ApiQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
/**
 * 问大家相关控制器
 */

@Controller
@RequestMapping(value = "${apiPath}/2.0/question")
public class ApiQuestionController extends Api20Controller {


    @Autowired
    private ApiQuestionService questionService;


    /**
     * 发布问大家
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addQuestion", method = RequestMethod.POST)
    public Result addQuestion(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.addQuestion(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取问大家列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getQuestionAll", method = RequestMethod.POST)
    public Result getQuestionAll(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.getQuestionAll(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取问大家详情
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getQuestionDetail", method = RequestMethod.POST)
    public Result getQuestionDetail(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.getQuestionDetail(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取问大家列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getQuestionList", method = RequestMethod.POST)
    public Result getQuestionList(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.getQuestionList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取我的问大家列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMyQuestionList", method = RequestMethod.POST)
    public Result getMyQuestionList(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.getMyQuestionList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取我参于问大家列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMyAnswerQuestionList", method = RequestMethod.POST)
    public Result getMyAnswerQuestionList(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.getMyAnswerQuestionList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }


    /**
     * 发布问大家的回答
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addAnswer", method = RequestMethod.POST)
    public Result addAnswer(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            //图片上传到阿里云
            return questionService.addAnswer(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取回答列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAnswerList", method = RequestMethod.POST)
    public Result getAnswerList(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.getAnswerList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 发布回答的回复
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addReply", method = RequestMethod.POST)
    public Result addReply(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.addReply(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取评论回答列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getReplyList", method = RequestMethod.POST)
    public Result getReplyList(Response<QuestionRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return questionService.getReplyList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
}
